package com.jcl.cloud.services.crawler.brain.config;


import com.jcl.cloud.services.catchemail.client.CatchEmailClient;
import com.jcl.cloud.services.catchnew.client.CatchNewClient;
import com.jcl.cloud.services.crawler.brain.service.GrabBuyResumeWorkerService;
import com.jcl.cloud.services.crawler.brain.service.GrabCrawlerWorkerService;
import com.jcl.cloud.services.crawler.brain.task.BuyResumeWorkerHandler;
import com.jcl.cloud.services.crawler.brain.task.GrabCrawlerWorkerHandler;
import com.jcl.cloud.services.crawler.brain.task.WorkerDispatcher;
import com.jcl.cloud.services.crawler.brain.task.WorkerType;
import com.jcl.cloud.services.login.client.LoginWorkerClient;
import com.jcl.cloud.services.search.client.SearchWorkerClient;
import com.jcl.cloud.services.unicrawler.client.CrawlerManagerClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class CrawlerBrainConfig {

    @Autowired
    private GrabCrawlerWorkerService grabCrawlerWorkerService;
    @Autowired
    private GrabCrawlerWorkerHandler grabCrawlerWorkerHandler;
    @Autowired
    private CrawlerGrabProperties crawlerGrabProperties;

    @Autowired
    private GrabBuyResumeWorkerService grabBuyResumeWorkerService;
    @Autowired
    private BuyResumeWorkerHandler buyResumeWorkerHandler;


    @Autowired(required = true)
    private RedisConfigurationProperties redisConfigurationProperties;


    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


    @Bean
    @ConditionalOnBean(RestTemplate.class)
    public CrawlerManagerClient crawlerManagerClient(RestTemplate restTemplate) {
        return new CrawlerManagerClient(restTemplate);
    }

    @Bean
    @ConditionalOnBean(RestTemplate.class)
    public LoginWorkerClient loginWorkerClient(RestTemplate restTemplate){
        return new LoginWorkerClient(restTemplate);
    }


    @Bean
    @ConditionalOnBean(RestTemplate.class)
    public CatchNewClient catchNewClient(RestTemplate restTemplate){
        return new CatchNewClient(restTemplate);
    }



    @Bean
    @ConditionalOnBean(RestTemplate.class)
    public SearchWorkerClient searchWorkerClient(RestTemplate restTemplate){
        return new SearchWorkerClient(restTemplate);
    }



    @Bean
    @ConditionalOnBean(RestTemplate.class)
    public CatchEmailClient catchEmailClient(RestTemplate restTemplate){
        return new CatchEmailClient(restTemplate);
    }



    @Bean(
            name = {"grabWorkerDispatcher"}
    )
    public WorkerDispatcher grabWorkerDispatcher(){
        WorkerDispatcher grabWorkerDispatcher = new WorkerDispatcher(WorkerType.GRAB_TASK, this.crawlerGrabProperties.getGrabCoreSize(), this.crawlerGrabProperties.getGrabMaxSize(), this.crawlerGrabProperties.getGrabQueueSize(), this.crawlerGrabProperties.getKeepAliveTime(), this.crawlerGrabProperties.getGrabNoTaskSleepSeconds(), this.crawlerGrabProperties.getGrabHungrySize());
        grabWorkerDispatcher.setWorkerService(this.grabCrawlerWorkerService);
        grabWorkerDispatcher.setWorkerHandler(this.grabCrawlerWorkerHandler);
        return grabWorkerDispatcher;
    }


    @Bean(
            name = {"grabBuyResumeWorkerDispatcher"}
    )
    public WorkerDispatcher grabBuyResumeWorkerDispatcher(){
        WorkerDispatcher grabWorkerDispatcher = new WorkerDispatcher(WorkerType.GRAB_BUY_RESUME_TASK, this.crawlerGrabProperties.getGrabCoreSize(), this.crawlerGrabProperties.getGrabMaxSize(), this.crawlerGrabProperties.getGrabQueueSize(), this.crawlerGrabProperties.getKeepAliveTime(), this.crawlerGrabProperties.getGrabNoTaskSleepSeconds(), this.crawlerGrabProperties.getGrabHungrySize());
        grabWorkerDispatcher.setWorkerService(this.grabBuyResumeWorkerService);
        grabWorkerDispatcher.setWorkerHandler(this.buyResumeWorkerHandler);
        return grabWorkerDispatcher;
    }



}
